package com.vq.controller;

import com.vq.common.R;
import com.vq.entity.FriendList;
import com.vq.entity.User;
import com.vq.service.FriendListService;
import com.vq.service.UserService;
import com.vq.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/FriendList")
public class FriendListController {
    @Autowired
    HttpSession session;

    @Autowired
    UserService userService;
    @Autowired
    FriendListService friendListService;

    @Autowired
    EmailUtil emailUtil;

    /**
     * 添加好友
     * @param friendAccount
     * @return
     */
    @PostMapping("/{friendAccount}")
    @ResponseBody
    public R<FriendList> insert(@PathVariable String friendAccount) throws MessagingException {
        User myUser = (User) session.getAttribute("myUser");
        if(myUser==null) return new R<>(0);
        FriendList friendList = new FriendList(myUser.getAccount(), friendAccount);
        boolean bo = friendListService.insert(friendList);
        if(bo) {
            SimpleMailMessage message = new SimpleMailMessage();
            String text = "<div style=\"height: 60vh; background-color: #f7f7f7;\">" +
                    "        <table style=\"width: 100%; height: 30%; margin-top: 10%;\">" +
                    "            <tr align='center'>" +
                    "                <td style=\"font-size: 150%;\">"+myUser.getUsername()+"（"+myUser.getAccount()+"） 关注了你</td>" +
                    "            </tr>" +
                    "        </table>" +
                    "        <table style=\"width: 100%; height: 20%;\">" +
                    "            <tr align=\"center\">" +
                    "                <td><a href=\"http://43.140.192.152\">进入官网</a></td>" +
                    "            </tr>" +
                    "        </table>" +
                    "    </div>";
            User friendUser = userService.selectByAccount(friendAccount, false);
            message.setTo(friendUser.getEmail());
            message.setSubject("新粉丝");
            message.setText(text);
            emailUtil.send(message, true);
            return new R<>(1, friendList);
        } else {
            return new R<>(0);
        }
    }

    /**
     * 删除好友
     * @param account
     * @return
     */
    @DeleteMapping("/{account}")
    @ResponseBody
    public R deleteFriend(@PathVariable String account) {
        User user = (User) session.getAttribute("myUser");
        User friend = userService.selectByAccount(account, false);
        R r = new R(0);
        if(user!=null && friend!=null) {
            FriendList friendList = new FriendList(user.getAccount(), friend.getAccount());
            boolean bo = friendListService.isEmpty(friendList);
            if(!bo) {
                friendListService.deleteByAll(friendList);
                r.setCode(1);
            }
            //添加日志
            log.info("删除："+friendList);
        }
        return r;
    }

    /**
     * 查询关注列表
     * @return
     */
    @GetMapping("/1")
    @ResponseBody
    public R<?> select() {
        User user = (User) session.getAttribute("myUser");
        R<List<User>> r = new R<>(0);
        if(user == null) return r;
        List<User> users = friendListService.selectByMyAccount(user.getAccount());
        r.setCode(1);
        r.setData(users);
        return r;
    }
    /**
     * 查询粉丝列表
     * @return
     */
    @GetMapping("/2")
    @ResponseBody
    public R<?> selectFans() {
        User user = (User) session.getAttribute("myUser");
        R<List<User>> r = new R<>(0);
        if(user == null) return r;
        List<User> users = friendListService.selectFansByMyAccount(user.getAccount());
        r.setCode(1);
        r.setData(users);
        return r;
    }
    /**
     * 查询好友
     * @return
     */
    @GetMapping("/3")
    @ResponseBody
    public R<List<User>> selectAllFriend() {
        User user = (User) session.getAttribute("myUser");
        R<List<User>> r = new R<>(0);
        if(user == null) return r;
        List<User> users = friendListService.selectFriendByMyAccount(user.getAccount());
        r.setCode(1);
        r.setData(users);
        return r;
    }
}
